<?php
class Blogger {

    public function spin_content($content) {
        $zz = substr_count($content, "}");
        $m  = 0;
        while ($m < $zz) {
            $nn     = strpos($content, "}");
            $y      = substr($content, 0, $nn);
            $p      = strrpos($y, "{");
            $bdata  = substr($content, $p, $nn - $p + 1);
            $bdata2 = eregi_replace("{|}", "", $bdata);
            $cspin  = explode("|", $bdata2);
            shuffle($cspin);
            $newspin = $cspin[0];
            $content = str_replace($bdata, $newspin, $content);
            ++$m;
        }
        return $content;
    }
    public function post($param , $xml) {
        $asin = $xml->Items->Item->ASIN;

        $title = $param['title'];
        $content = $param['content'];
        $keyword = $param['keyword'];
		$tacking_id = $param['tacking_id'];

        $obj_blogger = new Blogger();
        $rTitle = $obj_blogger->getXMLToBlog($xml, $title ,$keyword ,$tacking_id);
        $rContet = $obj_blogger->getXMLToBlog($xml, $content, $keyword, $tacking_id);
        
        /* Spin Content */
        $rTitle = Blogger::spin_content($rTitle);
        $rContet = Blogger::spin_content($rContet);

        /* Post to Blogger */
        $client = Zend_Gdata_ClientLogin::getHttpClient($param['email'], $param['password'], 'blogger', null,
                Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null,
                Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');
        $gdClient = new Zend_Gdata($client);

        $query = new Zend_Gdata_Query('http://www.blogger.com/feeds/default/blogs');
        $feed = $gdClient->getFeed($query);

        $uri = 'http://www.blogger.com/feeds/' . $param['blog_id'] . '/posts/default';
        $entry = $gdClient->newEntry();
        $entry->title = $gdClient->newTitle($rTitle);
        $entry->content = $gdClient->newContent($rContet);
        $entry->content->setType('text');

        $createdPost = $gdClient->insertEntry($entry, $uri);
        $idText = split('-', $createdPost->id->text);
        $newPostID = $idText[2];

        return $newPostID;
    }

    public function getXMLToBlog($xml , $value , $Keyword, $tacking_id) {
        /* TAG
		{ASIN} , {Title} , {Keyword} , {AmazonURL} , {Features} ,
        {Rating} , {ListPrice} , {Price}, {AmountSaved}, {PercentageSaved},
		{Content}, {ReviewsIframe},{ReviewsNoIframe}, {SmallImage}, {MediumImage},
		{LargeImage}
         *
        */
        $data = "";
        $item = $xml->Items->Item;
        $iFeature = $item->ItemAttributes->Feature;
        /* Get From XML */

		$ASIN = $item->ASIN;
        $Title = $item->ItemAttributes->Title;
        $iContent = $item->EditorialReviews->EditorialReview;
        $AmazonURL = "http://www.amazon.com/gp/product/" . $ASIN . "?tag=" . $tacking_id;
        $SmallImage = $item->SmallImage->URL;
        $MediumImage = $item->MediumImage->URL;
        $LargeImage = $item->LargeImage->URL;
        $ListPrice = $item->ItemAttributes->ListPrice->FormattedPrice;
        //$Price = $item->OfferSummary->LowestNewPrice->FormattedPrice;
		$Price = $item->Offers->Offer->OfferListing->Price->FormattedPrice;
		$AmountSaved = $item->Offers->Offer->OfferListing->AmountSaved->FormattedPrice;
		$PercentageSaved = $item->Offers->Offer->OfferListing->PercentageSaved;

        /*** Product Review ***/
        $product_review = $item->CustomerReviews->IFrameURL;

		/*** Review IFrame ***/
        $reviewsiframe = '<iframe style="margin-top: 10px;" src="'.$product_review.'" width="580px" height="500px" scrolling="auto" marginwidth="0" marginheight="0" frameborder="0"><p>Your browser does not support iframes.</p></iframe>';
		/*** End Review IFrame ***/

		/*** Review NoIFrame ***/
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$product_review);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
        $revcontent = curl_exec($ch);
        curl_close($ch);

        $revcontent = ereg_replace('[[:space:]]+', ' ', trim($revcontent));

        if (preg_match('~<body[^>]*>(.*?)</body>~si', $revcontent, $body)) {
            $reviewsnoiframe = str_replace('class="crVotingButtons">', "", $body[1]);
        } else {
            $reviewsnoiframe = "";
        }

        if($reviewsnoiframe != "") {
            $reviewsnoiframe = str_replace('class="crIFrame"', ' style="margin:0px;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameLogo"', ' style="margin-bottom:20px;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameHeaderLeftColumn"', ' style="float:left;margin-right:20px;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIframeHeaderTitle"', ' style="font-size:24px;letter-spacing:-1px;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameAverageReviewText"', ' style="font-weight:bold;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameNumCustReviews"', ' style="white-space:nowrap;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameHeaderHistogram"', ' style="float:left;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="tiny"', ' style=font-family:verdana,arial,helvetica,sans-serif;font-size:x-small;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIframeReviewList"', ' style="clear:left;width:100%;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('<a ', ' <a rel="nofollow" ', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('<div style="padding-top: 10px; clear: both; width: 100%;"', ' <div style="padding-top: 10px; clear: both; width: 100%;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameCreateReview"', ' style="display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="tiny verifyWhatsThis"', ' style="display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('href="/gp/help/customer/display.html/', ' href="http://www.amazon.'.$site.'/gp/help/customer/display.html/', $reviewsnoiframe);

            if ($Keyword == "") {
                $reviewsnoiframe = '<h3>'.$item->ItemAttributes->Title.' Reviews</h3></br>'.$reviewsnoiframe;
            }else {
                $reviewsnoiframe = '<h3>'.$Keyword.' : '.$item->ItemAttributes->Title.' Reviews</h3></br>'.$reviewsnoiframe;
            }

        }
		/*** End Review NoIFrame ***/

        /*** Customer Rating ***/
        $customer_rating_url = $item->DetailPageURL;

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$customer_rating_url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
        $customer_rating_data  = curl_exec($ch);
        curl_close($ch);

        $customer_rating_data = explode("out of 5 stars", $customer_rating_data);
        $customer_rating = str_replace('" >','',$customer_rating_data[1]);
        $customer_rating = str_replace('<span>','',$customer_rating);

        $Rating = Blogger::wpr_star_rating($customer_rating);
		/*** End Customer Rating ***/

		/*** Content ***/
        $Content = "";
        foreach (@$iContent as $val) {
            $Content .='<h2>'.$val->Source.'</h2>';
            $Content .='<p>'.$val->Content.'</p>';
        }
		/*** End Content ***/

		/*** Features ***/
        $Features = "<ul>";
        foreach (@$iFeature as $val) {
            $Features .="<li>".$val."</li>";
        }
        $Features .="</ul>";
		/*** End Features ***/

		
        $data = str_replace('{Title}', $Title, $value);
		$data = str_replace('{ASIN}', $ASIN, $data);
        $data = str_replace('{Keyword}', $Keyword, $data);
        $data = str_replace('{Content}', $Content, $data);
        $data = str_replace('{Features}', $Features, $data);
        $data = str_replace('{AmazonURL}', $AmazonURL, $data);
        $data = str_replace('{ListPrice}', $ListPrice, $data);
        $data = str_replace('{Price}', $Price, $data);
		$data = str_replace('{AmountSaved}', $AmountSaved, $data);
		$data = str_replace('{PercentageSaved}', $PercentageSaved, $data);	
        $data = str_replace('{SmallImage}', $SmallImage, $data);
        $data = str_replace('{MediumImage}', $MediumImage, $data);
        $data = str_replace('{LargeImage}', $LargeImage, $data);
        $data = str_replace('{Rating}', $Rating, $data);
        $data = str_replace('{ReviewsIframe}', $reviewsiframe, $data);
		$data = str_replace('{ReviewsNoIframe}', $reviewsnoiframe, $data);

        return $data;
    }



    public function wpr_star_rating($rating) {

        if($rating>=0 && $rating <= 0.7) {
            $image = '<img src="https://lh4.googleusercontent.com/-wpKtkP77nDk/URMkE5UNFTI/AAAAAAAAATE/_X7JA10g5Fo/h68/rating_0.5.gif" alt="Rating: '.$rating.'" class="starrating" />';
        }
        if($rating>=1.3 && $rating <= 1.7) {
            $image = '<img src="https://lh6.googleusercontent.com/-HIqQeRkC1rM/URMkP7sPH0I/AAAAAAAAATM/i2gmjLt9vl4/h68/rating_1.0.gif" alt="Rating: '.$rating.'" class="starrating" />';
        }
        if($rating>=2.3 && $rating <= 2.7) {
            $image = '<img src="https://lh3.googleusercontent.com/-9c7HpniOUQU/URMkZW7CUaI/AAAAAAAAATU/sqtCDgyrSpY/h68/rating_1.5.gif" alt="Rating: '.$rating.'" class="starrating" />';
        }
        if($rating>=3.3 && $rating <= 3.7) {
            $image = '<img src="https://lh5.googleusercontent.com/-iM4NWkOv01Q/URMkkXvIlYI/AAAAAAAAATc/7KPNMU5ishk/h68/rating_2.0.gif" alt="Rating: '.$rating.'" class="starrating" />';
        }
        if($rating>=4.3 && $rating <= 4.7) {
            $image = '<img src="https://lh3.googleusercontent.com/-xFmoqntVhJo/URMktTrCKrI/AAAAAAAAATk/9b3sgagjJQw/h68/rating_2.5.gif" alt="Rating: '.$rating.'" class="starrating" />';
        }
        if($rating>=0.8 && $rating <= 1.2) {
            $image = '<img src="https://lh5.googleusercontent.com/-WvmaaK3N_uk/URMlGm7rF5I/AAAAAAAAATs/Iy3eihca53I/h68/rating_3.0.gif" alt="Rating: '.$rating.'" class="starrating" />';
        }
        if($rating>=1.8 && $rating <= 2.2) {
            $image = '<img src="https://lh4.googleusercontent.com/-UjiXSJv8pQo/URMlNeojxBI/AAAAAAAAAT0/m96S5nQYlRo/h68/rating_3.5.gif" alt="Rating: '.$rating.'" class="starrating" />';
        }
        if($rating>=2.8 && $rating <= 3.2) {
            $image = '<img src="https://lh6.googleusercontent.com/-zZ2ioujYX74/URMlT2ZPttI/AAAAAAAAAT8/gvmcF92lxrU/h68/rating_4.0.gif" alt="Rating: '.$rating.'" class="starrating" />';
        }
        if($rating>=3.8 && $rating <= 4.2) {
            $image = '<img src="https://lh4.googleusercontent.com/-0moFGDek_48/URMmJdSf1RI/AAAAAAAAAUI/Wf0Uz-rfqcw/h68/rating_4.5.gif" alt="Rating: '.$rating.'" class="starrating" />';
        }
        if($rating>=4.8 && $rating <= 5) {
            $image = '<img src="https://lh6.googleusercontent.com/-Sql_xTii3ko/URMmQzyXDGI/AAAAAAAAAUQ/egdZgcpnr4M/h68/rating_5.0.gif" alt="Rating: '.$rating.'" class="starrating" />';
        }

        return $image;
    }
}
?>
